package vsvm.gui.controls;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JList;
import javax.swing.JPopupMenu;

import vsvm.classifier.AbstractTestSet;
import vsvm.gui.menus.TestSetMenu;

public class TestSetList extends JList {
	private static final long serialVersionUID = 100L;
	
	
	private class MouseHandler extends MouseAdapter {

		@Override
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger() && isEnabled()) {
				setSelectedIndex(locationToIndex(e.getPoint()));
				showPopup(e.getX(), e.getY());
			}
		}	
		
		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger() && isEnabled()) {
				setSelectedIndex(locationToIndex(e.getPoint()));
				showPopup(e.getX(), e.getY());
			}
		}
		
	}
	
	
	public TestSetList() {
		super();
		addMouseListener(new MouseHandler());
	}
	
	private void showPopup(int x, int y) {
		if (getSelectedValue() == null) return;
		JPopupMenu popup = TestSetMenu.getPopup((AbstractTestSet)getSelectedValue());
		popup.show(this, x, y);		
	}

}
